<?php

class CProductPrice extends Zend_Db_Table{
	
	protected $product;
	
	protected $_name;
	
	protected $_primary;
	
	public function _setup(){
		
		$this->_name=R_PRE."product_price";
		
		$this->_primary="priceid";
		
		parent::_setup();
		
	}
	
	public function createPrice($data){
		
		$priceid=$this->insert($data);
		
		return $priceid;
		
	}
	
	public function getAllPrice($itemid,$where=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where('itemid=?',$itemid);
		
		if($where!=''){
			
			$select->where($where);
			
		}
		
		$select->order("priceid ASC");
		
		$result =  $db->fetchAll($select);
		
		//用户存在该坑位
        $uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
        if ($uid) {
        	$flashCommon = new CFlashcommon();
        	if ($flashCommon->isUserFlashItem($uid, $itemid) || $flashCommon->isUserWhenEverFlashItem($uid, $itemid)) {
        		$flashSaleCommon = new CFlashsales();
        		$discount = $flashSaleCommon->getDiscount($itemid);
        		$return = array();
        		$productAttr = new CProductattr();
        		if ($productAttr->getAttr($itemid, 11)) {
        			$i = 0;
        			foreach ($result as $v) {
        				if (count($result) == 4) {
	        				if ($i == 1) {
		        				$v['supply_price'] = 28.99;
		        				$v['net_price'] = 28.99;
	        				}
	        				if ($i == 2) {
	        					$v['supply_price'] = 25.99;
		        				$v['net_price'] = 25.99;
	        				}
	        				if ($i == 3) {
	        					$v['supply_price'] = 23.99;
		        				$v['net_price'] = 23.99;
	        				}
	        				$i++;
        				}
	        			$return[] = $v;
	        		}
        		} elseif ($productAttr->getAttr($itemid, 12)) {
        			$i = 0;
        			foreach ($result as $v) {
        				if (count($result) == 4) {
	        				if ($i == 1) {
		        				$v['supply_price'] = 33.99;
		        				$v['net_price'] = 33.99;
	        				}
	        				if ($i == 2) {
	        					$v['supply_price'] = 30.99;
		        				$v['net_price'] = 30.99;
	        				}
	        				if ($i == 3) {
	        					$v['supply_price'] = 28.99;
		        				$v['net_price'] = 28.99;
	        				}
	        				$i++;
        				}
	        			$return[] = $v;
	        		}
        		} else {
	        		$newPrice = number_format($result[0]['supply_price'] * $discount,2);
	        		foreach ($result as $v) {
	        			$v['supply_price'] = $newPrice;
	        			$v['net_price'] = $newPrice;
	        			$return[] = $v;
	        		}
        		}
        		$result = $return;
        	}
        }
        //
		return $result;
	}

	
	/* $data=array(
					'0'=>array('priceid'=>'1','itemid'=>'1','min_counts'=>'dffs'),
					'1'=>array('priceid'=>'2','itemid'=>'2','min_counts'=>'2323')
				   )
	表单中attachmentid为空的是新增附件(执行插入操作), 不为空的则为之前的附件(执行修改操作)
	*/
	public function editPrice($data){
		
		$db=$this->getAdapter();
		
		foreach($data as $valueArray){
			
			if($valueArray['priceid']!=''){
				
				$where = $db->quoteInto('priceid = ?', $valueArray['priceid']);
				
				unset($valueArray['priceid']);
				
				unset($valueArray['itemid']);
	
				$this->update($valueArray,$where);
				
			}else{
				
				unset($valueArray['priceid']);
				
				$this->insert($valueArray);
				
			}
			
		}
		
	}
	
	public function getDetail($priceid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where('priceid = ? ',$priceid);
		
		return $db->fetchAll($select);
		
	}
	
	public function delPrices($priceidArray){
		
		$db=$this->getAdapter();
		
		$priceidString=join(',',$priceidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE priceid IN ($priceidString)");
		
		return $result;
		
	}
	
	public function delPricesByItemid($itemidArray){
		
		$db=$this->getAdapter();
		
		$itemidString=join(',',$itemidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE itemid IN ($itemidString)");
		
		return $result;
		
	}
	
}
?>